<%--
  Created by IntelliJ IDEA.
  User: Purif
  Date: 2020/9/12
  Time: 22:58
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>

<div class="layui-row">
    <div class="layui-col-md6 layui-col-lg-offset3"
         style="text-align: center;font-size: larger; font-weight: bolder;margin-top: 20px;margin-bottom: 20px;">
        公司管理
    </div>
</div>

<div class="layui-row" style="width: 96%;margin: 0 auto;">
    <div class="layui-col-md12" style="">
        <table class="layui-hide" lay-filter="filterId1" id="addressId"></table>
    </div>
</div>

<script type="text/html" id="toolbar1">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm" lay-event="getCheckData">获取选中行数据</button>
        <button class="layui-btn layui-btn-sm" lay-event="getCheckLength">获取选中数目</button>
        <button class="layui-btn layui-btn-sm" lay-event="isAll">验证是否全选</button>
        <button class="layui-btn layui-btn-sm" lay-event="isAll2" data-type="auto" data-method="offset" id="insertBtn">新增</button>
    </div>
</script>

<script type="text/html" id="barId1">
    <a class="layui-btn layui-btn-xs" lay-event="edit">修改</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>

<script>
    layui.use('table', function () {
        var table = layui.table;

        table.render({
            elem: '#addressId'
            , cellMinWidth: 80 //全局定义常规单元格的最小宽度，layui 2.2.1 新增
            , url: '/company/findAll'
            , toolbar: '#toolbar1' //开启头部工具栏，并为其绑定左侧模板
            , defaultToolbar: ['filter', 'exports', 'print', { //自定义头部工具栏右侧图标。如无需自定义，去除该参数即可
                title: '提示'
                , layEvent: 'LAYTABLE_TIPS'
                , icon: 'layui-icon-tips'
            }]
            , cols: [[
                {type: 'checkbox'}
                , {field: 'cid', title: 'ID', sort: true}
                , {field: 'logo', title: 'logo', align: 'center'} //width 支持：数字、百分比和不填写。你还可以通过 minWidth 参数局部定义当前单元格的最小宽度，layui 2.2.1 新增
                , {field: 'name', title: '公司名称', align: 'center'}
                , {field: 'listedstate', title: '上市状态', align: 'center'}
                , {field: 'scale', title: '规模', sort: true}
                , {field: 'classify', title: '分类', align: 'center'}
                , {field: 'address', title: '地址', align: 'center'}
                , {field: 'addpic', title: '地址图片', align: 'center'}
                , {field: 'introduce', title: '介绍', align: 'center'}
                , {field: 'iacinfo', title: '工商信息', align: 'center'}
                , {fixed: 'right', title: '操作', toolbar: '#barId1', width: 150}
            ]]
            , page: true //开启分页
        });

        //头工具栏事件
        table.on('toolbar(filterId1)', function (obj) {
            var checkStatus = table.checkStatus(obj.config.id);
            switch (obj.event) {
                case 'getCheckData':
                    var data = checkStatus.data;
                    layer.alert(JSON.stringify(data));
                    break;
                case 'getCheckLength':
                    var data = checkStatus.data;
                    layer.msg('选中了：' + data.length + ' 个');
                    break;
                case 'isAll':
                    layer.msg(checkStatus.isAll ? '全选' : '未全选');
                    break;

                //自定义头工具栏右侧图标 - 提示
                case 'LAYTABLE_TIPS':
                    layer.alert('这是工具栏右侧自定义的一个图标按钮');
                    break;
            }
            ;
        });

        //监听行工具事件
        table.on('tool(filterId1)', function (obj) {
            var data = obj.data;
            //console.log(obj)
            if (obj.event === 'del') {
                layer.confirm('真的删除行么'+data.cid, function (index) {
                    $.ajax({
                        url : '/company/delete/'+data.cid,
                        type : 'get',
                        dataType : 'json',
                        success : function(res) {//List<User>
                            layer.msg("删除成功", {icon: 6});
                        },
                        error:function(err){
                            layer.msg("删除失败", {icon: 6});
                        }
                    });
                    obj.del();
                    layer.close(index);
                });
            } else if (obj.event === 'edit') {
                layer.prompt({
                    formType: 0,
                    value: data.cid,
                    title: '修改',
                    area: ['1500px', '300px'] //自定义文本域宽高

                }, function(value, index, elem){
                    //alert(value); //得到value

                    /*if($('#nameId').val()===""){
                        layer.tips("请填写地址名称",$('#nameId'));
                        return;
                    }
                    if($('#categoryId').val()===""){
                        layer.tips("请填写分类级别",$('#categoryId'));
                        return;
                    }
                    if($('#fcategoryId').val()===""){
                        layer.tips("请填写所属分类",$('#fcategoryId'));
                        return;
                    }*/
                    alert("修改---------------"+data.cid);

                    $.ajax({
                        type : 'get',
                        url : '/company/update',
                        data : {
                            cid : data.cid,
                            logo : $("#logoId").val(),
                            name : $("#nameId").val(),
                            listedstate : $("#listedstateId").val(),

                            scale : $("#scaleId").val(),
                            classify : $("#classifyId").val(),
                            address : $("#addressId").val(),

                            addpic : $("#addpicId").val(),
                            introduce : $("#introduceId").val(),
                            iacinfo : $("#iacinfoId").val(),
                        },
                        dataType : 'json',
                        success : function(res) {//List<User>
                            layer.msg("修改成功", {icon: 6});
                            window.location.reload();
                        },
                        error:function(err){
                            //alert("修改失败"+res.code)
                            ayer.msg("修改失败", {icon: 6});
                        }
                    });
                    layer.close(index);
                });

                $(".layui-layer-content").append(
                    "<br/><input type=\"text\" id= \"logoId\" class=\"layui-input\" placeholder=\"输入logo\" value='"+data.logo+"'/>");
                $(".layui-layer-content").append(
                    "<br/><input type=\"text\" id= \"nameId\" class=\"layui-input\" placeholder=\"输入公司名称\" value='"+data.name+"'/>");
                $(".layui-layer-content").append(
                    "<br/><input type=\"text\" id= \"listedstateId\" class=\"layui-input\" placeholder=\"输入上市状态\" value='"+data.listedstate+"'/>");

                $(".layui-layer-content").append(
                    "<br/><input type=\"text\" id= \"scaleId\" class=\"layui-input\" placeholder=\"输入规模\" value='"+data.scale+"'/>");
                $(".layui-layer-content").append(
                    "<br/><input type=\"text\" id= \"classifyId\" class=\"layui-input\" placeholder=\"输入分类\" value='"+data.classify+"'/>");
                $(".layui-layer-content").append(
                    "<br/><input type=\"text\" id= \"addressId\" class=\"layui-input\" placeholder=\"输入地址\" value='"+data.address+"'/>");

                $(".layui-layer-content").append(
                    "<br/><input type=\"text\" id= \"addpicId\" class=\"layui-input\" placeholder=\"输入地址图片\" value='"+data.addpic+"'/>");
                $(".layui-layer-content").append(
                    "<br/><input type=\"text\" id= \"introduceId\" class=\"layui-input\" placeholder=\"输入介绍\" value='"+data.introduce+"'/>");
                $(".layui-layer-content").append(
                    "<br/><input type=\"text\" id= \"iacinfoId\" class=\"layui-input\" placeholder=\"输入工商信息\" value='"+data.iacinfo+"'/>");
            }
        });
    });
    /*  */
    var formStr = '<form class="layui-form" action="/company/insert" method="post">\n' +
        '<div class="layui-form-item">\n' +
        '    <label class="layui-form-label">Logo</label>\n' +
        '    <div class="layui-input-block">\n' +
        '      <input type="text" name="logo" id="logoInId" lay-verify="" lay-reqtext="logo是必填项，岂能为空？" placeholder="请输入" autocomplete="off" class="layui-input">\n' +
        '    </div>\n' +
        '</div>\n' +
        '<div class="layui-form-item">\n' +
        '    <label class="layui-form-label">公司名称</label>\n' +
        '    <div class="layui-input-block">\n' +
        '       <input type="text" name="name" id="nameInId" lay-verify="" lay-reqtext="公司名称是必填项，岂能为空？" placeholder="请输入" autocomplete="off" class="layui-input">\n' +
        '    </div>\n' +
        '</div>\n' +
        '\n' +
        '<div class="layui-form-item">\n' +
        '      <label class="layui-form-label">上市状态</label>\n' +
        '      <div class="layui-input-block">\n' +
        '        <input type="text" name="listedstate" id="listedstateInId" lay-verify="" lay-reqtext="上市状态是必填项，岂能为空？" placeholder="请输入" autocomplete="off" class="layui-input">\n' +
        '      </div>\n' +
        '</div>\n' +
        '\n' +

        '<div class="layui-form-item">\n' +
        '    <label class="layui-form-label">规模</label>\n' +
        '    <div class="layui-input-block">\n' +
        '      <input type="text" name="scale" id="scaleInId" lay-verify="" lay-reqtext="规模是必填项，岂能为空？" placeholder="请输入" autocomplete="off" class="layui-input">\n' +
        '    </div>\n' +
        '</div>\n' +
        '<div class="layui-form-item">\n' +
        '    <label class="layui-form-label">分类</label>\n' +
        '    <div class="layui-input-block">\n' +
        '       <input type="text" name="classify" id="classifyInId" lay-verify="" lay-reqtext="分类是必填项，岂能为空？" placeholder="请输入" autocomplete="off" class="layui-input">\n' +
        '    </div>\n' +
        '</div>\n' +
        '\n' +
        '<div class="layui-form-item">\n' +
        '      <label class="layui-form-label">地址</label>\n' +
        '      <div class="layui-input-block">\n' +
        '        <input type="text" name="address" id="addressInId" lay-verify="" lay-reqtext="地址是必填项，岂能为空？" placeholder="请输入" autocomplete="off" class="layui-input">\n' +
        '      </div>\n' +
        '</div>\n' +
        '\n' +

        '<div class="layui-form-item">\n' +
        '    <label class="layui-form-label">地址图片</label>\n' +
        '    <div class="layui-input-block">\n' +
        '      <input type="text" name="addpic" id="addpicInId" lay-verify="" lay-reqtext="地址图片是必填项，岂能为空？" placeholder="请输入" autocomplete="off" class="layui-input">\n' +
        '    </div>\n' +
        '</div>\n' +
        '<div class="layui-form-item">\n' +
        '    <label class="layui-form-label">介绍</label>\n' +
        '    <div class="layui-input-block">\n' +
        '       <input type="text" name="introduce" id="introduceInId" lay-verify="" lay-reqtext="介绍是必填项，岂能为空？" placeholder="请输入" autocomplete="off" class="layui-input">\n' +
        '    </div>\n' +
        '</div>\n' +
        '\n' +
        '<div class="layui-form-item">\n' +
        '      <label class="layui-form-label">工商信息</label>\n' +
        '      <div class="layui-input-block">\n' +
        '        <input type="text" name="iacinfo" id="iacinfoInId" lay-verify="" lay-reqtext="工商信息是必填项，岂能为空？" placeholder="请输入" autocomplete="off" class="layui-input">\n' +
        '      </div>\n' +
        '</div>\n' +
        '\n' +
        '<div class="layui-form-item">\n' +
        '    <div class="layui-input-block">\n' +
        '      <button type="submit" class="layui-btn" lay-submit="" lay-filter="filterSubmit" id="insertSubId">立即提交</button>\n' +
        '      <button type="reset" class="layui-btn layui-btn-primary">重置</button>\n' +
        '    </div>\n' +
        '</div>\n' +
        '</form>';
    //cid,logo,name,listedstate,scale,classify,address,addpic,introduce,iacinfo
    //id,logo,公司名称,上市状态,规模,分类（写死）,地址,地址图片,介绍,工商信息
    layui.use('layer', function () { //独立版的layer无需执行这一句
        var $ = layui.jquery, layer = layui.layer; //独立版的layer无需执行这一句
        //触发事件
        var active = {
            offset: function(othis){
                var type = othis.data('type')
                    /*,text = othis.text();*/
                    ,text = "123456";
                layer.open({
                    type: 1
                    ,offset: type //具体配置参考：http://www.layui.com/doc/modules/layer.html#offset
                    ,id: 'layerDemo'+type //防止重复弹出
                    ,content: '<div style="padding: 20px 100px;">'+ formStr +'</div>'
                    /* ,btn: '关闭全部'
                    ,btnAlign: 'c' //按钮居中*/
                    ,shade: 0.5 //不显示遮罩
                    ,yes: function(){
                       /* $.ajax({
                            type : 'get',
                            url : '/address/insert',
                            data : {
                                name : $("#nameInId").val(),
                                category : $("#categoryInId").val(),
                                fcategory : $("#fcategoryInId").val()
                            },
                            dataType : 'json',
                            success : function(res) {//List<User>
                                layer.msg("添加成功", {icon: 6});
                                window.location.reload();
                            },
                            error:function(err){
                                //alert("修改失败"+res.code)
                                ayer.msg("添加失败", {icon: 6});
                            }
                        });*/
                        layer.closeAll();
                    }
                    ,area: ['500px', '600px']
                });
            }
        };
        $('#insertBtn').on('click', function () {
            var othis = $(this), method = othis.data('method');
            active[method] ? active[method].call(this, othis) : '';
        });
        $('#insertSubId').on('click', function () {
            var othis = $(this), method = othis.data('method');
            active[method] ? active[method].call(this, othis) : '';
        });
    });


    layui.use(['form', 'layedit', 'laydate'], function () {
        var form = layui.form
            , layer = layui.layer
            , layedit = layui.layedit
            , laydate = layui.laydate;

        //自定义验证规则
        form.verify({
            nameverify: function (value) {
                if (value.length < 2) {
                    return '地址至少得2个字符啊';
                }
            }
        });

        //监听提交
        form.on('submit(filterSubmit)', function (data) {
            /*layer.alert(JSON.stringify(data.field), {
                title: '最终的提交信息'
            })*/
            return true;
        });
    });
</script>

<form>

</form>